import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        //当前socket监听的port
        ServerSocket serverSocket = new ServerSocket(55533);
        //获取到客户端发送的socket
        Socket socket = serverSocket.accept();
        InputStream inputStream ;
        OutputStream outputStream = null;
        while (true){
            inputStream = socket.getInputStream();
            int first = inputStream.read();
            if (first == -1){
                break;
            }
            int second = inputStream.read();
            int length = (first<<8) + second;
            byte[] bytes = new byte[length];
            inputStream.read(bytes);
            System.out.println(new String(bytes,"UTF-8"));
            String msg = input.next();
            outputStream = socket.getOutputStream();
            byte[] outputBytes = msg.getBytes("UTF-8");
            outputStream.write(outputBytes.length>>8);
            outputStream.write(outputBytes.length);
            outputStream.write(outputBytes);
            outputStream.flush();
        }
        outputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}
